package com.example.budget3.recurring;

import java.util.List;
import java.util.Locale;

import com.astuetz.viewpager.extensions.PagerSlidingTabStrip;
import com.example.budget3.R;
import com.example.budget3.months.MainActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;

public class RecurringManager extends FragmentActivity {

	public static final String ARG_SECTION_NUMBER = "sectionNumber";

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recurring_manager);


		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		mViewPager.setOffscreenPageLimit(3);
		PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		 tabs.setViewPager(mViewPager);
		 tabs.setTextColorResource(R.color.large_button_text);

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == MainActivity.RESULT_EDIT_RECURRING && resultCode == RESULT_OK) {
			if (data.getBooleanExtra("updated", false)) {
				checkForChanges();
			}
		}
	}
	
	
    private void checkForChanges() {
    	List<Fragment> fragments = getSupportFragmentManager().getFragments();
    	for(Fragment frag: fragments){
    		if(frag instanceof RecurrListFragment){
    			((RecurrListFragment)frag).checkForChanges();
    		}
    	}
		
	}

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {

		// Handle action buttons
	    switch(item.getItemId()) {
		    case android.R.id.home:
		    	finish();
		    	return true;
		    default:
		        return super.onOptionsItemSelected(item);
	    }
	
    }

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			// getItem is called to instantiate the fragment for the given page.
			// Return a DummySectionFragment (defined as a static inner class
			// below) with the page number as its lone argument.
			Fragment fragment = new RecurrListFragment();
			Bundle args = new Bundle();
			args.putInt("period", position);
			fragment.setArguments(args);
			return fragment;
		}
		
		@Override
		public int getCount() {
			// Show 3 total pages.
			return 3;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return getString(R.string.title_section1).toUpperCase(l);
			case 1:
				return getString(R.string.title_section2).toUpperCase(l);
			case 2:
				return getString(R.string.title_section3).toUpperCase(l);
			}
			return null;
		}
	}

}
